package com.controller.survey;





import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.controller.vo.LossVO;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.Caseone;
import com.pojo.CaseoneExample;
import com.pojo.CaseoneExample.Criteria;
import com.pojo.Discribe;
import com.pojo.Loss;
import com.service.caseoneservice.Caseoneservice;
import com.service.discribe.DiscribeService;
import com.service.lossservice.Lossservice;

@Controller
public class OwnLossController {
   @Autowired
   private Lossservice lossservice;
   @Autowired
   private DiscribeService discribeService;
   @Autowired
   private Caseoneservice caseoneservice;
	
	
    @RequestMapping("/ownloss")
    public String ownLoss(){
        
   
        
        
        return "own_loss";
    }
	
	@RequestMapping("update_ownLoss")
	public String updateownLoss(String id,Model model){
		Loss loss = lossservice.selectByPrimaryKey(Integer.parseInt(id));
		
		LossVO lossVO = lossservice.getLossVO(loss);
		
		model.addAttribute("lossVO", lossVO);
		
		
		return "add_ownLoss";
	};
	
    @RequestMapping(value="/doLossShow", produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public String doLossShow(@RequestParam Integer pageNo, String lossType, String lossName,String id){
    	PageHelper.startPage(pageNo,5);
    	
    	List<LossVO> list = lossservice.getLossVO(lossType, lossName);
	    	
    	PageInfo<LossVO> page=new PageInfo<LossVO>(list);	
	    	
	    return JSON.toJSONString(page);
	 }
	
	 
	 @RequestMapping("/doloss")
	 public String doLoss(String[] discribe,String[] money,String lossid){
		 Loss loss = lossservice.selectByPrimaryKey(Integer.parseInt(lossid));
		 loss.setDecidetime(new Date());
		 lossservice.updateByPrimaryKey(loss);
		 
		 
		 
		if (discribe!=null) {
			for (int i = 0; i < discribe.length; i++) {
				
				Discribe discribe2 = new Discribe();
				String string = discribe[i];
				String string2 = money[i];
				System.out.println(string);
				System.out.println(string2);
				discribe2.setDiscribe(string);
				discribe2.setMoney(Integer.parseInt(string2));
				discribe2.setLossid(lossid);
				discribeService.insert(discribe2);
				
			}
		}
		
		String caseno = loss.getCaseno();
		CaseoneExample caseoneExample = new CaseoneExample();
		Criteria criteria = caseoneExample.createCriteria();
		criteria.andCasenoEqualTo(caseno);
		List<Caseone> list = caseoneservice.selectByExample(caseoneExample);
		Caseone caseone = list.get(0);
		caseone.setStatusid(7);
		caseoneservice.updateByPrimaryKey(caseone);
	 
		
		return "redirect: ownloss.html"; 
	 };
    
    
}
